
const _c = require('../../../constants.js');
const util = require('../../../utils/app.js');

import { wxLogin, address, articleCollect, getSeverUserInfo, getPhoneNumber } from "../../../api/eapi.js";

Page({
  data: {
    userData: {},
    person: {
      name: 'Wong .',
      avatar: _c.DEFAULT_AVATAR
    },
    collections: [],
    icons: [{ img: '/images/order_btn_all_normal@2x.png', text: '全部订单' },
    { img: '/images/order_btn_pay_normal@2x.png', text: '待付款' },
    { img: '/images/order_btn_collect_normal@2x.png', text: '待收货' },
    { img: '/images/order_btn_finish_normal@2x.png', text: '已完成' }],
    defaultAddress: []
  },
  //事件处理函数
  bindViewTap: function () {
    console.log('点击事件发生')
  },
  bindArrow: function (e) {
    switch (e.currentTarget.dataset.text) {
      case '我的订单':
        wx.navigateTo({
          url: "../oders/index?active=all"
        });
        break;
      case '我的收藏':
        wx.navigateTo({
          url: "../collection/index"
        });
        break;
      default: ;
    }
  },
  iconTap: function (e) {
    let active;
    switch (e.currentTarget.dataset.text) {
      case '全部订单':
        active = 'all';
        break;
      case '待付款':
        active = 'pay';
        break;
      case '待收货':
        active = 'recieve';
        break;
      case '已完成':
        active = 'all';
        break;
      default: ;
    }
    wx.navigateTo({
      url: "../oders/index?active=" + active
    })
  },
  onShow() {
    wx.getStorage({
      key: `me_address_change`,
      success: (res) => {
        if (res.data === `true`) {
          address.get().then(resp => {
            this.setData({ defaultAddress: resp.data.filter(item => item.default === 1) }, () => wx.setStorage({ key: `me_address_change`, data: `false` }))
          }).catch(err => {
            if (err.code === `401`) address.get().then(resp => this.setData({ defaultAddress: resp.data.filter(item => item.default === 1) }, () => wx.setStorage({ key: `me_address_change`, data: `false` })));
            console.error(err);
          })
        }
      }
    });
    wx.getStorage({
      key: `me_collection_change`,
      success: (res) => {
        if (res.data === `true`) {
          articleCollect.get().then(resp => {
            let collection=[];
            for (let a = 0; a < resp.data.length; a++) {
              if (!resp.data[a].article.feature) {
                collection.push(resp.data[a])
              }
            }
            this.setData({
              collections:collection  }, () => wx.setStorage({ key: `me_collection_change`, data: `false` }));
             
          })
        }
      }
    })
  },
  onLoad: function () {
    wx.showLoading({ title: `加载中` });
    // wxLogin().then(resp => {
    // 加载个人信息
    getSeverUserInfo().then(resp => {
      this.setData({ userData: resp.data }, () => wx.hideLoading());
    })

    // 加载默认地址
    address.get().then(resp => {
      this.setData({ defaultAddress: resp.data.filter(item => item.default === 1) }, () => wx.setStorage({ key: `me_address_change`, data: `false` }))
    })

    // 加载收藏
    articleCollect.get().then(resp => {
      let  collection=[];
        resp.data.forEach( item =>{
          if(item.article){
            if(!item.article.feature){
              collection.push(item)
            }
          }
        })


      this.setData({
        collections:collection }, () => wx.setStorage({ key: `me_collection_change`, data: `false` }));
      
    })
   

  },
  getPhone(e) {
    let _this = this;
    function check() {
      wx.showLoading({ title: `加载中` });
      getPhoneNumber(e.detail).then(res => {

        getSeverUserInfo().then(resp => {
          _this.setData({ userData: resp.data }, () => wx.hideLoading());
        })
      }).catch(resp => {
        wx.showToast({
          title: '错误'
        }); wx.hideLoading()
      })
    }

    wx.checkSession({
      success: function () {
        check()
      },
      fail: function () {
        wx.showModal({
          title: '提示',
          content: '登录已过期',
          cancelText: '重新登录',
          confirmText: '取消',
          success: function (res) {
            if (res.confirm) {
              wx.reLaunch({
                url: '../../QRcode/index',
              })
            }
          }
        })
      }
    })
  }
})
